package utilities 
{
	
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.FlxDelay;
	
	/**
	 * Countdown for levels. To use, create a new countdown and center it if you wish and just add it to the current state.
	 * 
	 * @author Andrew Lau
	 */
	public class Countdown extends FlxGroup
	{
		
		private var text:FlxText;
		private var count:uint;
		private var delay:FlxDelay;
		private var done:Boolean;
		
		public function Countdown(start:uint = 3, x:Number = 0, y:Number = 0, size:uint = 64, width:uint = 600) 
		{
			text = new FlxText(x, y, width, "" + start);
			text.alignment = "center";
			text.size = size;
			text.color = FlxG.WHITE;
			
			add(text);
			
			count = start;
			
			delay = new FlxDelay(1000);
			delay.start();
			
			done = false;
		}
		
		override public function update():void
		{
			super.update();
			
			if (!done && delay.hasExpired) {
				if (count >= 1) {
					if(--count > 0) {
						text.text = "" + count;
					} else {
						text.text = "GO";			
					}
					delay.reset(1000);
					delay.start();
				} else {
					remove(text);
					done = true;
				}
			}
		}
		
		public function center():Countdown
		{
			text.x = (FlxG.width - text.width) / 2;
			text.y = (FlxG.height - text.size) / 2;
			
			return this;
		}
		
		public function get finished():Boolean
		{
			return done;
		}
	}

}